<template>
  <div>
    <el-card>
      <bread first="个人中心" second="基本资料"></bread>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        label-width="100px"
      >
        <el-form-item label="登录名称" prop="username">
          <el-input disabled v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="ruleForm.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="ruleForm.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm()">提交修改</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import bread from '../../components/bread.vue'
import { changeUserinfo } from '@/api'
import _ from 'lodash'
export default {
  components: { bread },
  name: '',
  data () {
    return {
      ruleForm: {
        nickname: '',
        email: '',
        username: ''
      },
      rules: {
        username: [{ message: '请输入用户名', trigger: 'blur' }],
        nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }]
      }
    }
  },
  created () {
    // this.ruleForm = JSON.parse(JSON.stringify(this.$store.state.userInfo))
    this.ruleForm = _.cloneDeep(this.$store.state.userInfo)
  },
  computed: {},
  methods: {
    submitForm () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const res = await changeUserinfo(this.ruleForm)
          if (res.data.code !== 0) return this.$message.error('更新失败')
          this.$message.success('更新成功')
          this.$store.dispatch('userInfoHandle')
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang='less'  scoped>
.el-form {
  width: 500px;
  margin-top: 20px;
}
</style>
